<?php
declare (strict_types=1);

namespace app\listener;

use app\common\job\ConsultingEnd;
use app\common\model\Consulting;
use app\common\model\ConsultingMessage;
use app\common\model\ConsultingServer;
use app\index\dao\ConsultingSubsidyDao;
use app\index\service\MessageSendService;
use think\facade\Queue;

class ConsultingCreated
{

    /**
     * 咨询被创建 事件监听处理
     * 免费咨询 创建时触发；付费咨询 支付后触发
     * @return mixed
     */
    public function handle(Consulting $consulting)
    {
        $robotId = $consulting->robotId;

        // 发送机器人消息
        $content = [
            'text' => '您好，请问您遇见了什么法律问题？简单描述您的问题（不少于8个字）'
        ];
        if (is_id($consulting->lawyerId)) {
            $this->getMessageSendService()->sendMemberMessage($consulting, 1, $consulting->lawyerId, ConsultingMessage::TEXT, $content);
        } else {
            $this->getMessageSendService()->sendRobotMessage($consulting, $robotId, 0, ConsultingMessage::TEXT, $content);
        }

        // 24小时后尝试关闭
        $job_data = ['id' => $consulting->id];
        Queue::later(24 * 3600 + 5, ConsultingEnd::class, $job_data);
    }

    private function getMessageSendService(): MessageSendService
    {
        return app(MessageSendService::class);
    }

}
